// 上传图片到相册

// 获得上传相片页面容器
const upABimgBox = document.getElementsByClassName("upABimgBox")[0];

// 获得预览图片的盒子
const lookUpImgBox = document.getElementsByClassName("lookUpImgBox")[0];

// 获得上传照片的按钮
const upLoadImgABbtn = document.getElementById("upImgABbtn");

// 获得提交相片内容的按钮
const submitAbImgBtn = document.getElementById("submitAbImg");

// 相册上传的预览盒
function previewImg(allFile){
    //len获得图片数量
    let len = allFile.files.length;
    let strImg = '';
    //  给图片占位
        for(let i = 0 ; i<len ; i++){
            strImg += `<img class="previewImg" src="./img/blank.png" alt="">`
        }
    // 放入容器中
        lookUpImgBox.innerHTML = strImg;
    for(let i = 0 ; i <len ; i++){
        var reader = new FileReader();
        // 读取每个文件的路径
        reader.readAsDataURL(allFile.files[i]);
        reader.onload = function(e){
            var img = document.getElementsByClassName("previewImg")[i];
            // 给每一个文件设置上传路径
            img.src = e.target.result;
        }
    }  
}

// 点击上传照片的模板
upLoadImgABbtn.onclick = function(){
    upABimgBox.style.display = "flex";
    createAbBox.style.display = "none";
    albumDetailBox.style.display = "none"; 
    createAbBox.style.display = "none";
    lookAlbumBox.style.display = "none"; 
}

// 确认上传的按键
submitAbImgBtn.onclick = function(){
    let ImgFiles = document.getElementById("upABimg");
    let upAbId = Number(document.getElementById("upAbId").value);

    let data = new FormData();
    data.append("albumId",upAbId);
    for(let i = 0 ; i < ImgFiles.files.length;i++){
        data.append("file", ImgFiles.files[i]);
    }

    if(upAbId && ImgFiles){
        postForm(data,"upload").then(function(value){
            if(value.code == 4000){
                alert("加入图片成功！");
                lookABbtn.click();
            }
            else if(value.code == 4001){
                alert(value.msg);
            }
        },function(reason){console.log(reason)})
    }
    else{
        console.log("请检查输入的相册ID是否有误")
    }

}